package com.prime.tutorial.moviestore.view.customer;

import com.prime.tutorial.moviestore.domain.CustomerGroups;
import com.prime.tutorial.moviestore.domain.Customers;
import com.prime.tutorial.moviestore.service.customer.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: vasylja
 * Date: May 6, 2010
 * Time: 8:30:54 PM
 *
 * @author vasylja
 */
@Component("customerCreate")
@Scope("request")
public class CustomerCreate implements Serializable {
    private CustomerService customerService;

    //used to generate a list of groups for select menu
    private List availableCustomerGroups;

    private Customers customerToCreate;
//TODO Check for problems.
    //used to store the result of select
    private String selectedCustomerGroup;

    @Autowired
    public CustomerCreate(CustomerService customerService) {
        this.setCustomerService(customerService);
        this.setCustomerToCreate(new Customers());
        //list of group values for select list is created
        setAvailableCustomerGroups(new ArrayList());
        List<CustomerGroups> customerGroups = customerService.getAllCustomerGroups();
        for(int i=0; i<customerGroups.size();i++){
            SelectItem option = new SelectItem(customerGroups.get(i).getName(), customerGroups.get(i).getName());
            getAvailableCustomerGroups().add(option);
        }
    }
    //------------------------------------public methods
    public String createCustomer(ActionEvent actionEvent){
        //fields are not filled in
        if (getCustomerToCreate().getLogin().equals("") ||
            getCustomerToCreate().getPassword().equals("") ||
            getCustomerToCreate().getFirstName().equals("") ||
            getCustomerToCreate().getLastName().equals("")
                ) {
            FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_INFO, "Some of the required fields are not filled in", "");
            FacesContext.getCurrentInstance().addMessage(null, facesMessage);
            return null;
        }

        //user with this login already exists
        if (getCustomerService().loginExists(getCustomerToCreate().getLogin())){
            FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_INFO, "Users with this login is already in the database", "");
            FacesContext.getCurrentInstance().addMessage(null, facesMessage);
            return null;
        }

        //everything is OK - getting customergroup
        List<CustomerGroups> g = getCustomerService().findByName(getSelectedCustomerGroup());
        getCustomerToCreate().setCustomerGroup(g.get(0));

        //setting dates
        getCustomerToCreate().setCreated(new Date());

        //TODO what is validity??
        getCustomerToCreate().setValidity(new Date());
        String text = getCustomerToCreate().getLogin();
        //adding user
        getCustomerService().createNew(getCustomerToCreate());

        setCustomerToCreate(new Customers());

 //       showMessage("Devices are added successfully");
         FacesContext context = FacesContext.getCurrentInstance();
        //TODO Find out how not to display the message twice.
         context.addMessage(null, new FacesMessage("Registered successfully: " + text));
    //     context.addMessage(null, new FacesMessage("Second Message", "Additional Info Here..."));
  //      FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_INFO, "Users is created successfully", "");
  //      FacesContext.getCurrentInstance().addMessage(null, facesMessage);
        return null;
    }


    //------------------------getters&setters
    public List getAvailableCustomerGroups() {
        return availableCustomerGroups;
    }

    public String getSelectedCustomerGroup() {
            return selectedCustomerGroup;
        }

    public void setSelectedCustomerGroup(String selectedCustomerGroup) {
            this.selectedCustomerGroup = selectedCustomerGroup;
        }

    public CustomerService getCustomerService() {
        return customerService;
    }

    public void setCustomerService(CustomerService customerService) {
        this.customerService = customerService;
    }

    public Customers getCustomerToCreate() {
        return customerToCreate;
    }

    public void setCustomerToCreate(Customers customerToCreate) {
        this.customerToCreate = customerToCreate;
    }

    public void setAvailableCustomerGroups(List availableCustomerGroups) {
        this.availableCustomerGroups = availableCustomerGroups;
    }
}
